// components/uploadImage/index.ts
import {uploadImage} from '../../api/work_order'
import { LENGYU_SUCCESS_CODE } from '../../utils/request'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    imgUrlStr:{
      type:String,
      value:""
    },
   
    addImgUrl:{
      type:String,
      value:"/images/icon/start.jpg",
    },
    imgHeight:{
      type:String,
      value:"60px",
    },
    imgWidth:{
      type:String,
      value:"60px",
    }
  },
  observers:{
    imgUrlStr:function(value){
      if(!value){
        this.setData({
          imgUrlList:[]
        })
      }
      else{
        console.log("图片URL变更",value)
        this.setData({
          imgUrlList:value.split(",")
        })
      }
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    imgUrlList:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    selectFile(e:any){
      console.log("选择文件",e)
      let that= this
      wx.chooseMedia({
        count: 9,
        mediaType: ['image','video'],
        sourceType: ['album', 'camera'],
        maxDuration: 30,
        camera: 'back',
        success(res) {
          console.log(res.tempFiles[0].tempFilePath)
          console.log(res.tempFiles[0].size)
          that.uploadFile(res.tempFiles[0].tempFilePath);
        }
      })
    },
  async  uploadFile(url:string){
      let result1 = await uploadImage(url,{})
      if(result1.code == LENGYU_SUCCESS_CODE){
        this.setData({
          imgUrlList:this.data.imgUrlList.concat(result1.data)
        })
        console.log("触发事件",result1)
        this.triggerEvent('dataChanged', {imgUrlList:this.data.imgUrlList}) 
      }
      else{
        wx.showToast({
          title:'文件上传失败'+ result1.msg,
          icon:'error',
          duration:1500
        })
      }
    },
    deleteImage(e:any){
      console.log("删除图片",e.currentTarget.dataset.index)
      let index = e.currentTarget.dataset.index;
      let myList = this.data.imgUrlList
      myList.splice(index,1)   
      this.setData({
        imgUrlList: myList
      })
      this.triggerEvent('dataChanged', {imgUrlList:this.data.imgUrlList}) 
    }
  },

})